# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from web_XZDQ_V1_0 import Common


def QLGL_BHTJ_index(request):
    return render(request, 'QLGL/BHTJ/QLGL_BHTJ_index.html', {})


# 构件病害数量
def BHTJ_GJSL_index(request):
    bridgeinfo = Common.ConverToDICT(Common.GetAllBridge(), ["bridge_id", "bridge_code", "bridge_name"])
    return render(request, 'QLGL/BHTJ/GJSL.html', {"bridgeinfo": bridgeinfo})


def BHTJ_GJBH_TREE_DATA(request):
    bridge_id = request.POST.get("bridge_id", 0)
    f_date = request.POST.get("f_date", '')
    t_date = request.POST.get("t_date", '')
    displayall = int(request.POST.get("display_all", '0'))
    list = []

    # 构造测试数据
    # list.append(dict(parts_code=0, parts_name=u"上部承重构件（主梁、挂梁）", disease_count=0, parts_sub=[
    #     dict(parts_sub_id=0, parts_sub_name=u"T梁", disease_count=0,
    #          member=[dict(member_id=0, member_name="T梁xx", disease_count=0)])]))
    # list.append(dict(parts_code=1, parts_name=u"上部一般构件（湿接缝、横隔板等）", disease_count=5, parts_sub=[
    #     dict(parts_sub_id=5, parts_sub_name=u"横隔板", disease_count=3,
    #          member=[dict(member_id=0, member_name="横隔板1", disease_count=1),
    #                  dict(member_id=0, member_name="横隔板2", disease_count=2)]),
    #     dict(parts_sub_id=7, parts_sub_name=u"铰缝", disease_count=1,
    #          member=[dict(member_id=0, member_name="铰缝1", disease_count=0),
    #                  dict(member_id=0, member_name="铰缝2", disease_count=1),
    #                  dict(member_id=0, member_name="铰缝3", disease_count=0)])]))
    try:
        # if False:
        row = Common.GetBridgeType(bridge_id)
        if len(row) < 1:
            raise Exception("请填写桥梁上部结构信息")
        bridge_type = row[0][0]
        # 构造数据，数据分三层，1：部件；2：子部件；3：构件；
        # 桥梁部件
        row = Common.GetPartsListByBridge(bridge_id)
        memberdisease = Common.ConverToDICT(Common.GetBridgeMemberDiseaseByDate(bridge_id, f_date, t_date),
                                            ["bridge_member_id", "member_no", "member_name", "parts_code",
                                             "parts_sub_id", "disease_count"])
        if len(row) > 0:
            for i in range(len(row)):
                # 子部件
                parts_code = row[i][1]
                parts_sub = Common.ConverToDICT(Common.GetPartsSub(bridge_type, parts_code),
                                                ["parts_sub_id", "param_bridge_type_code", "parts_code",
                                                 "position_code", "name"])
                ps = []
                totalcount = 0
                for subpart in parts_sub:
                    # 构件
                    if displayall == 0:
                        minfo = [w for w in memberdisease if
                                 w["parts_sub_id"] == subpart["parts_sub_id"] and w["disease_count"] > 0]
                    else:
                        minfo = [w for w in memberdisease if w["parts_sub_id"] == subpart["parts_sub_id"]]
                    count = sum([w["disease_count"] for w in minfo])
                    if displayall == 0:
                        if count > 0:
                            ps.append(dict(parts_sub_id=subpart["parts_sub_id"], parts_sub_name=subpart["name"],
                                           disease_count=count, member=minfo))
                    else:
                        ps.append(dict(parts_sub_id=subpart["parts_sub_id"], parts_sub_name=subpart["name"],
                                       disease_count=count, member=minfo))

                totalcount = sum([w["disease_count"] for w in ps])
                if displayall == 0:
                    if totalcount > 0:
                        list.append(
                            dict(parts_code=parts_code, parts_name=row[i][2], disease_count=totalcount, parts_sub=ps))
                else:
                    list.append(
                        dict(parts_code=parts_code, parts_name=row[i][2], disease_count=totalcount, parts_sub=ps))

        data = dict(status=1, message=u"查询成功", data=list)
    except Exception as e:
        data = dict(status=0, message=u"查询失败", data=[])

    # print data
    return JsonResponse(data)


# 桥梁病害数量
def BHTJ_QLBHSL_INDEX(request):
    return render(request, 'QLGL/BHTJ/QLBHSL.html', {})


# 桥梁病害类型
def BHTJ_QLBHLX_INDEX(request):
    bridgeinfo = Common.ConverToDICT(Common.GetAllBridge(), ["bridge_id", "bridge_code", "bridge_name"])
    return render(request, 'QLGL/BHTJ/QLBHLX.html', {"bridgeinfo": bridgeinfo})


# 部件病害类型
def BHTJ_BJBHLX_INDEX(request):
    bridgeinfo = Common.ConverToDICT(Common.GetAllBridge(), ["bridge_id", "bridge_code", "bridge_name"])
    return render(request, 'QLGL/BHTJ/BJBHLX.html', {"bridgeinfo": bridgeinfo})


# 日巡病害程度
def BHTJ_RXBHCD_INDEX(request):
    return render(request, 'QLGL/BHTJ/RXBHCD.html', {})


# 严重病害详情
def BHTJ_YZBHXQ_INDEX(request):
    return render(request, 'QLGL/BHTJ/YZBHXQ.html', {})


# 指定病害范围
def BHTJ_ZDBHFW_INDEX(request):
    bridgeinfo = Common.ConverToDICT(Common.GetAllBridge(), ["bridge_id", "bridge_code", "bridge_name"])
    return render(request, 'QLGL/BHTJ/ZDBHFW.html', {"bridgeinfo": bridgeinfo})

# 桥梁病害数量数据
def BHTJ_QLBHSL_DATA(request):
    f_date = request.POST.get("f_date", '')
    t_date = request.POST.get("t_date", '')
    ldata = []
    try:
        data = dict(status=1, message=u"查询成功", data=ldata)
    except Exception as e:
        data = dict(status=0, message=u"查询失败", data=[])
    return JsonResponse(data)


# 桥梁病害类型数据
def BHTJ_QLBHLX_DATA(request):
    f_date = request.POST.get("f_date", '')
    t_date = request.POST.get("t_date", '')
    ldata = []
    try:
        data = dict(status=1, message=u"查询成功", data=ldata)
    except Exception as e:
        data = dict(status=0, message=u"查询失败", data=[])
    return JsonResponse(data)

# 部件病害类型数据
def BHTJ_BJBHLX_DATA(request):
    f_date = request.POST.get("f_date", '')
    t_date = request.POST.get("t_date", '')
    ldata = []
    try:
        data = dict(status=1, message=u"查询成功", data=ldata)
    except Exception as e:
        data = dict(status=0, message=u"查询失败", data=[])
    return JsonResponse(data)


# 日巡病害程度数据
def BHTJ_RXBHCD_DATA(request):
    f_date = request.POST.get("f_date", '')
    t_date = request.POST.get("t_date", '')
    ldata = []
    try:
        data = dict(status=1, message=u"查询成功", data=ldata)
    except Exception as e:
        data = dict(status=0, message=u"查询失败", data=[])
    return JsonResponse(data)


# 严重病害详情数据
def BHTJ_YZBHXQ_DATA(request):
    f_date = request.POST.get("f_date", '')
    t_date = request.POST.get("t_date", '')
    ldata = []
    try:
        data = dict(status=1, message=u"查询成功", data=ldata)
    except Exception as e:
        data = dict(status=0, message=u"查询失败", data=[])
    return JsonResponse(data)


# 指定病害范围数据
def BHTJ_ZDBHFW_DATA(request):
    f_date = request.POST.get("f_date", '')
    t_date = request.POST.get("t_date", '')
    ldata = []
    try:
        data = dict(status=1, message=u"查询成功", data=ldata)
    except Exception as e:
        data = dict(status=0, message=u"查询失败", data=[])
    return JsonResponse(data)
